package com.lk.mall.controller.admin;

import com.lk.mall.common.IndexConfigTypeEnum;
import com.lk.mall.config.annotation.TokenToAdminUser;
import com.lk.mall.model.entity.AdminUserToken;
import com.lk.mall.model.entity.IndexConfig;
import com.lk.mall.service.IIndexConfigService;
import com.lk.mall.util.PageQueryUtil;
import com.lk.mall.util.PageResult;
import com.lk.mall.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Lemonade
 * @description
 * @updateTime 2022/9/9 9:59
 */
@Api(value = "后台管理系统首页配置模块接口", tags = "IndexConfigAPI")
@Slf4j
@RestController
@RequestMapping("/manage-api/v1")
public class IndexConfigAPI {

    IIndexConfigService indexConfigService;

    public IndexConfigAPI(IIndexConfigService indexConfigService) {
        this.indexConfigService = indexConfigService;
    }


    @GetMapping("/indexConfigs")
    @ApiOperation("首页配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(name = "configType", value = "1-搜索框热搜 2-搜索下拉框热搜 3-(首页)热销商品 4-(首页)新品上线 5-(首页)为你推荐"),
            @ApiImplicitParam(name = "adminUser", value = "用户")
    })
    public R<PageResult<IndexConfig>> list(@RequestParam(required = false) Integer pageNumber,
                  @RequestParam(required = false) Integer pageSize,
                  @RequestParam(required = false) Integer configType,
                  @TokenToAdminUser AdminUserToken adminUser){

        log.info("当前管理员用户是：------------------->" + adminUser.toString());
        // 校验
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 10) {
            return new R<PageResult<IndexConfig>>().fail("分页参数异常！");
        }

        IndexConfigTypeEnum indexConfigTypeEnumByType = IndexConfigTypeEnum.getIndexConfigTypeEnumByType(configType);
        if (indexConfigTypeEnumByType.equals(IndexConfigTypeEnum.DEFAULT)){
            return new R<PageResult<IndexConfig>>().fail("非法参数！");
        }

        // 返回结果
        Map<String, Object> params = new HashMap<>();
        params.put("page", pageNumber);
        params.put("limit", pageSize);
        params.put("configType", configType);

        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        PageResult<IndexConfig> configsPage = indexConfigService.getConfigsPage(pageQueryUtil);

        return new R<PageResult<IndexConfig>>().ok(configsPage);
    }

}
